import request from '../../../utils/request';
Page({
  page: {
    pageSize: 10,
    pageNo: 1,
  },

  data: {
    status: '',
    tabs: [
      { key: '', text: '全部' },
      { key: 1, text: '正在进行' },
      { key: 2, text: '已结束' },
      { key: 3, text: '中拍' }
    ],
    listLoading: 0,
    pullDownRefreshing: false,
    list: [],
  },

  onLoad(query) {
    this.init(this.data.status);
    this.pullDownRefresh = this.selectComponent('#wr-pull-down-refresh');
  },
  init(status) {
    this.refreshList(status);
  },
  onShow() {
    // if (!this.data.backRefresh) return;
    // this.onRefresh();
    // this.setData({ backRefresh: false });
  },

  refreshList(status = '') {
    this.page = {
      pageSize: 10,
      pageNo: 1
    };
    this.setData({ status: status, list: [] });

    return Promise.all([
      this.getMoneyList(status, true),
    ]);
  },

  getMoneyList(statusCode = '', reset = false) {
    var that = this;
    const params = {
      pageNo: this.page.pageNo,
      pageSize: this.page.pageSize,
      orderType: 'desc',
      orderField: 'id'
    };
    if (statusCode != '') {
      if (statusCode == "3") {
        params.filter = [{
          field: "success",
          operator: "EQ",
          value: 1
        }]
      } else {
        params.filter = [{
          field: "auctionFlag",
          operator: "EQ",
          value: statusCode
        }]
      }
    }

    this.setData({ listLoading: 1 });
    var url = "/car/user/dealer/myAuction/pageData";
    request.post(url, params, function (res) {
      let list = [];
      if (res.code == 0) {
        that.page.pageNo++;
        if (res.data && res.data.rows) {
          list = res.data.rows;
        }
      }
      return new Promise((resolve) => {
        if (reset) {
          that.setData({ list: [] }, () => resolve());
        } else resolve();
      }).then(() => {
        that.setData({
          list: that.data.list.concat(list),
          listLoading: list.length > 0 ? 0 : 2,
        });
      });
    })
  },

  onReTryLoad() {
    this.getMoneyList(this.data.status);
  },

  onTabChange(e) {
    const { value } = e.detail;
    this.setData({
      status: value,
    });
    this.refreshList(value);
  },

  onRefresh() {
    this.refreshList(this.data.status);
  },

  onPullDownRefresh_(e) {
    const { callback } = e.detail;
    this.setData({ pullDownRefreshing: true });
    this.refreshList(this.data.status)
      .then(() => {
        this.setData({ pullDownRefreshing: false });
        callback && callback();
      })
      .catch((err) => {
        this.setData({ pullDownRefreshing: false });
        Promise.reject(err);
      });
  },

  goodListClickHandle(e) {
    const { goods } = e.detail;
    getApp().globalData.currentAuction = goods;
    wx.navigateTo({
      url: `/pages/goods/details/index?id=${goods.id}&type=${goods.type}&status=${goods.status}`,
    });
  },
});
